package com.jming.yygh.user.api;

import com.jming.yygh.common.result.Result;
import com.jming.yygh.common.util.AuthContextHolder;
import com.jming.yygh.model.user.Patient;
import com.jming.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 就诊人信息接口
 * @author jming
 * @version 0.0.1
 * @date 2021/05/12
 */
@Api(tags = "就诊人信息接口")
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Resource
    private PatientService patientService;

    /**
     * 获取就诊人信息
     * @param request
     * @return Result
     */
    @ApiOperation(value = "获取就诊人列表")
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request) {

        Long userId = AuthContextHolder.getUserId(request);

        List<Patient> list = patientService.findAllUserId(userId);
        return Result.ok(list);
    }

    /**
     * 添加就诊人信息
     * @param patient
     * @param request
     * @return Result
     */
    @ApiOperation(value = "添加就诊人信息")
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient,
                              HttpServletRequest request) {

        // 获取当前登录用户的id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }

    /**
     * 根据id获取就诊人信息
     * @param id
     * @return Result
     */
    @ApiOperation(value = "根据id获取就诊人信息")
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable Long id) {

        Patient patient = patientService.getPatientId(id);

        return Result.ok(patient);
    }

    /**
     * 修改就诊人信息
     * @param patient
     * @return Result
     */
    @ApiOperation(value = "修改就诊人信息")
    @PostMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient) {

        patientService.updateById(patient);
        return Result.ok();
    }

    /**
     * 根据id删除就诊人信息
     * @param id
     * @return Result
     */
    @ApiOperation(value = "根据id删除就诊人信息")
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable Long id) {

        patientService.removeById(id);

        return Result.ok();
    }

    /**
     * 根据id获取就诊人订单信息
     * @param id
     * @return Patient
     */
    @ApiOperation(value = "根据id获取就诊人订单信息")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable Long id) {
        return patientService.getPatientId(id);
    }

}
